<?php
App::uses('CakeEmail', 'Network/Email');
class ContactController extends AppController {
  //public $scaffold;
  public $helpers = array('Html', 'TWSForm');
  public $components = array('Session', 'Util', 'Email','Securimage');
  public $name = 'Contact';
  

//public version
  function index() {
    $this->layout = 'main';
    $this->set('title_for_layout', 'Contact Us');
    $this->set("meta_keywords","Contact Champaign Illinois Kennel Club, AKC, Dog club, Pure Bred Dogs, Central Illinois, Champaign");
    $this->set("meta_description","Contact the Champaign Illinois Kennel Club. We will be happy to provide you with information and resources to help you be a responsible and successful dog owner.");
    $this->loadModel('Content');
    $contentMap = Configure::read('App.contentPage');
    $this->Content->id = $contentMap["contactus"];
    $this->set('contents', $this->Content->read());

    $captcha_error_msg = 'The code you entered does not match';

    if ($this->request->is('post')) {
      $data = $this->_processRequestData($this->request);
      //checking the captcha
      $captchaResult = $this->Securimage->check($data["Contact"]["captcha_text"]);
      $this->Contact->set($data);
      if($this->Contact->validates() && $captchaResult) {
        $result = $this->_sendContactEmail($data['Contact']);
        if($result) {
          $this->Session->setFlash('The message was submitted successfully.');
          //$this->set('email',$result);
          $this->render('index');
        } else {
          throw new Exception('An error occurred and your message was not successfully sent. Please try again.');
        }
      } else {
        $captchaErrors = array();
        if (!empty($data['Contact']['captcha_text']) && !$captchaResult) {
          $captchaErrors   = array("captcha_text"=>array($captcha_error_msg));
        }
        $data['Contact']['captcha_text']="";
        $this->request->data['Contact'] = $data['Contact'];
        $this->set('errors', array_merge($this->Contact->validationErrors, $captchaErrors));
      }
    } 
  }

  function _sendContactEmail($data){
    $retVal = false;
    if(!empty($data)){
      $email = new CakeEmail('tws');
      $email->from(array(Configure::read('App.emailDefaultFrom')=>Configure::read('App.name')));
      $email->transport('Mail')->viewVars(array('website'=>Configure::read('App.websiteUrl'),'name' => $data['name'],'email'=>$data['email_address'], 'phone'=>$data['phone'],'subject'=>$data['subject'],'message'=>$data['message']));
      $retVal = $email->send();     
    }
    return $retVal;
  }

    /*
     *
     *  to
     * from
     * replyTo
     * return -- not sure if this is an email address
     * cc
     * bcc
     * subject
     * textMessage
     * htmlMessage
     * template - template for view
     * sendAs - text, html, both
     * smtpOptions
     * delivery mail, smtp, debug
     * attachments
     *
     */ 

}

?>
